#ifndef _ERP_GIS_FUNC_DEFINE_H_
#define _ERP_GIS_FUNC_DEFINE_H_

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "c2cplatform/library/the3/tconv/tconv_g2u.h"

namespace erp 
{
namespace gis
{
namespace comm
{

inline std::string IpToString(const uint32_t dwIp)
{
	struct in_addr stClient;
	memset(&stClient, 0, sizeof(struct in_addr));
	stClient.s_addr = htonl(dwIp);
	return inet_ntoa(stClient);
}

inline std::string Uint32ToString(const uint32_t dwValue)
{
	char ch[128] = {0};
	snprintf(ch, sizeof(ch), "%u", dwValue);

	return std::string(ch);
}

inline std::string Uint64ToString(const uint64_t ddwValue)
{
	char ch[128] = {0};
	snprintf(ch, sizeof(ch), "%lu", ddwValue);

	return std::string(ch);
}

inline std::string DoubleToString(const double dbValue)
{
	char ch[128] = {0};
	snprintf(ch, sizeof(ch), "%f", dbValue);

	return std::string(ch);
}

inline int GbkToUtf8(const std::string& strInputGbk, std::string& strOutputUtf8)
{
	strOutputUtf8.clear();

	std::size_t nSize = strInputGbk.size() * 3;
	char szOutput[nSize+1];
	memset(szOutput, 0, sizeof(szOutput));
	int iRet = tconv_gbk2utf8(strInputGbk.c_str(), strInputGbk.size(), szOutput, &nSize);
	if (iRet < 0)
	{
		return iRet;
	}

	strOutputUtf8.assign(szOutput);
	return 0;
}

inline int Utf8ToGbk(const std::string& strInputUtf8, std::string& strOutputGbk)
{
	strOutputGbk.clear();
	std::size_t nSize = strInputUtf8.size();
	char szOutput[nSize+1];
	memset(szOutput, 0, sizeof(szOutput));
	int iRet = tconv_utf82gbk(strInputUtf8.c_str(), strInputUtf8.size(), szOutput, &nSize);
	if (iRet < 0)
	{
		return iRet;
	}

	strOutputGbk.assign(szOutput);
	return 0;
}

#define DUMP_XXO_DEBUG_LOG(entityObj) do{\
	if (C2C_WWLOG->IsDebugLogOpen()) {\
		std::stringstream oss4Debug;\
		entityObj.DumpData(oss4Debug);\
		C2C_WW_LOG_DEBUG("####XXO:%s", oss4Debug.str().c_str());}\
}while(0);
		

#define C2C_WW_LOG_FERR(iRet, sformat,args...) \
		C2C_WW_LOG_FERR_PL35_V6(\
			iRet,\
			m_strLogModule.c_str(),\
			rCntlInfo,\
			strSource,\
			IpToString(rCntlInfo.getOperatorClientIP()),\
			strMachineKey,\
			Uint32ToString(rCntlInfo.getOperatorUin()),\
			Uint32ToString(rCntlInfo.getOperatorUin()),\
			sformat,\
			##args);

#define GET_AND_SET_CONFIG_UINT32(Config)do{\
	iRetcode = 0;\
	dwTemp = 0;\
	iRetcode=pCntl->getConfig(#Config,dwTemp);\
	if(iRetcode!=0)\
	{\
		std::cout<<#Config<<"="<<dw##Config<<" =default="<<std::endl;\
	}\
	else\
	{\
		dw##Config=dwTemp;\
		std::cout<<#Config<<"="<<dw##Config<<std::endl;\
	}\
}while(0)

#define GET_AND_SET_CONFIG_STRING(Config)do{\
	iRetcode = 0;\
	strTemp = "";\
	iRetcode=pCntl->getConfig(#Config, strTemp);\
	if(iRetcode!=0)\
	{\
		std::cout<<#Config<<"="<<str##Config<<" =default="<<std::endl;\
	}\
	else\
	{\
		str##Config=strTemp;\
		std::cout<<#Config<<"="<<str##Config<<std::endl;\
	}\
}while(0)

}
}
}

#endif /*_ERP_GIS_FUNC_DEFINE_H_*/

